package com.miss.spjyx

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import com.miss.spjyx.model.BaseRes
import com.miss.spjyx.network.CommonCallback
import com.miss.spjyx.network.Requester
import com.miss.spjyx.utils.UHfUtils
import com.miss.spjyx.widget.ProgressDialog
import com.uhf.scanlable.UHfData
import retrofit2.Call

/**
 * 出入库单详情
 *
 * @author wl
 * @since 2019/08/11 19:32
 */
class CRkuActivity : AppCompatActivity() {

    companion object {
        fun startThis(context: Context, ruku: Boolean, actId: String, fallId: String) {
            val intent = Intent(context, CRkuActivity::class.java)
            intent.putExtra("actId", actId)
            intent.putExtra("fallId", fallId)
            intent.putExtra("ruku", ruku)
            context.startActivity(intent)
        }
    }

    lateinit var adapter: CRkuAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_crku)

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        toolbar.setNavigationOnClickListener { finish() }
        val rv_data = findViewById<RecyclerView>(R.id.rv_data)
        adapter = CRkuAdapter(this)
        rv_data.adapter = adapter
        val tv_total = findViewById<TextView>(R.id.tv_total)
        val btn_upload = findViewById<Button>(R.id.btn_upload)
        btn_upload.setOnClickListener {
            upload()
        }

        UHfUtils.li = object : UHfUtils.onDataGotListener {
            override fun onDataChange(data: List<UHfData.InventoryTagMap>) {
                adapter.data = data
                tv_total.text = "共${data.size}"
            }
        }
    }

    override fun onResume() {
        super.onResume()
        UHfUtils.register(this)
    }

    override fun onPause() {
        super.onPause()
        UHfUtils.unregister(this)
    }

    override fun onDestroy() {
        super.onDestroy()
        UHfUtils.li = null
    }

    fun upload() {
        if (adapter.data.isNullOrEmpty()) {
            Toast.makeText(this, "没有数据需要上传", Toast.LENGTH_SHORT).show()
            return
        }
        ProgressDialog.show(this)
        val call: Call<BaseRes<String>>
        val actId = intent.getStringExtra("actId")
        val fallId = intent.getStringExtra("fallId")
        val spms = ArrayList<String>()
        adapter.data?.forEach {
            spms.add(it.strEPC)
        }
        call = if (intent.getBooleanExtra("ruku", true)) {
            Requester.apiService().inScan(actId, fallId, spms.joinToString())
        } else {
            Requester.apiService().outScan(actId, fallId, spms.joinToString())
        }
        call.enqueue(object : CommonCallback<String>(this) {
            override fun onData(data: String?) {
                Toast.makeText(this@CRkuActivity, "上传成功", Toast.LENGTH_SHORT).show()
            }

            override fun onDone() {
                super.onDone()
                ProgressDialog.dismiss()
            }
        })
    }
}